//
//  MineAddressViewController.m
//  renmingongshe
//
//  Created by macbook on 2017/11/2.
//  Copyright © 2017年 xiaodian. All rights reserved.
//

#import "MineAddressViewController.h"
#import "MineAddressTableViewCell.h"
#import "EditAddressViewController.h"

@interface MineAddressViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    NSMutableArray *_listArr;
}
@property (strong, nonatomic) UIView *footerView;
@property (strong, nonatomic) UITableView * tableView;
@property (copy, nonatomic) NSString *addressId;
@property (strong, nonatomic) UIButton *addBtn;
@end

@implementation MineAddressViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.titleLabel.text = @"地址管理";
    _listArr = [NSMutableArray array];
    [self downloadData];
    [self createFooterView];
    [self createTableView];
    [self createBottomView];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MineAddressRefresh) name:@"MineAddressRefresh" object:nil];
}

- (void)MineAddressRefresh
{
    [_listArr removeAllObjects];
    [self downloadData];
}

- (void)downloadData
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineAddress) Parameters:@{ @"userToken":[Utils getUserToken]} Success:^(id responseObject) {
        [self.view removeAnyView];
        if ([String(responseObject[@"status_code"]) isEqualToString:@"1000"]) {
            _addBtn.enabled = YES;
            [_addBtn setBackgroundColor:[UIColor redColor]];
            if ([responseObject[@"data"] count] != 0) {
                [_listArr addObjectsFromArray:responseObject[@"data"]];
            }else{
                [self.view Message:@"无更多数据"];
            }
        }else if ([String(responseObject[@"status_code"]) isEqualToString:@"1001"]){
            [self.view Message:@"暂无数据"];
        }else{
            [self.view Message:responseObject[@"msg"]];
            [_addBtn setBackgroundColor:[UIColor grayColor]];
            _addBtn.enabled = NO;
        }
        [_tableView reloadData];
        
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)createFooterView
{
    _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 1)];
    _footerView.backgroundColor = RGB(232, 234, 234);
}

- (void)createBottomView
{
    UIView *BottomView = [[UIView alloc]initWithFrame:CGRectMake(0, HEIGHT-120, WIDTH, 120)];
    BottomView.backgroundColor = [UIColor colorWithHex:0xf2f2f2];
    [self.view addSubview:BottomView];
    
    _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _addBtn.frame = CGRectMake(54, 40, WIDTH-108, 40);
    [_addBtn setBackgroundColor:[UIColor redColor]];
    [_addBtn setTitle:@"添加地址+" forState:UIControlStateNormal];
    _addBtn.layer.cornerRadius = 5.0;
    _addBtn.layer.masksToBounds = YES;
    [_addBtn addTarget:self action:@selector(AddClick) forControlEvents:UIControlEventTouchUpInside];
    [BottomView addSubview:_addBtn];
}

- (void)AddClick
{
    EditAddressViewController *edit = [[EditAddressViewController alloc] init];
    [self.navigationController pushViewController:edit animated:YES];
}

- (void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, WIDTH, HEIGHT-70-120)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"MineAddressTableViewCell" bundle:nil] forCellReuseIdentifier:@"MineAddressTableViewCell"];
    [_tableView setTableFooterView:_footerView];
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MineAddressTableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:@"MineAddressTableViewCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[MineAddressTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MineAddressTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.DefaultBtn.tag = indexPath.row;
    cell.EditBtn.tag = indexPath.row;
    cell.DeteleBtn.tag = indexPath.row;
    [cell.DefaultBtn addTarget:self action:@selector(defaultClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.EditBtn addTarget:self action:@selector(editBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell.DeteleBtn addTarget:self action:@selector(deleteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cell cellConfigureDic:_listArr[indexPath.row]];
    return cell;
}

- (void)defaultClick:(UIButton *)btn
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineDefaultAddress) Parameters:@{ @"userToken":[Utils getUserToken], @"addressId": _listArr[btn.tag][@"addressId"]} Success:^(id responseObject) {
        [self.view removeAnyView];
        [self.view Message:responseObject[@"msg"]];
        [_listArr removeAllObjects];
        [self downloadData];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)editBtnClick:(UIButton *)btn
{
    [self.view loadingOnAnyView];
    [THWManager PostHttp:API(MineEditAddress) Parameters:@{ @"userToken":[Utils getUserToken], @"addressId": _listArr[btn.tag][@"addressId"]} Success:^(id responseObject) {
        [self.view removeAnyView];
        EditAddressViewController *edit = [[EditAddressViewController alloc] init];
        edit.editType = @"edit";
        edit.AddressId = _listArr[btn.tag][@"addressId"];
        edit.userName = responseObject[@"data"][@"userName"];
        edit.userPhone = responseObject[@"data"][@"userPhone"];
        [self.navigationController pushViewController:edit animated:YES];
    } andFalse:^(NSError *error) {
        [self.view removeAnyView];
    }];
}

- (void)deleteBtnClick:(UIButton *)btn
{
    _addressId = _listArr[btn.tag][@"addressId"];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确定删除" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            break;
        }
        case 1:
        {
            [self.view loadingOnAnyView];
            [THWManager PostHttp:API(MineDeteleAddress) Parameters:@{ @"userToken":[Utils getUserToken], @"addressId": _addressId} Success:^(id responseObject) {
                [self.view removeAnyView];
                [self.view Message:responseObject[@"msg"]];
                [_listArr removeAllObjects];
                [self downloadData];
            } andFalse:^(NSError *error) {
                [self.view removeAnyView];
            }];
        }
        default:
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.address(_listArr[indexPath.row]);
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
